const puppeteer = require("puppeteer")

var search = async ctx=>{
    const browser = await puppeteer.launch({
        headless:false,
        defaultViewport:null
    });
    const page = await browser.newPage()
    var {kw} = ctx.query
    kw = encodeURIComponent(kw)
    await page.goto(`https://www.duitang.com/search/?kw=${kw}&type=feed`)
    await page.waitForTimeout(3000)
    var res = await page.evaluate(()=>{
        var $ = window.jQuery
        var arr = []
        var items = $(".woo-pcont .woo")
        items.each((index,value)=>{
            var id = $(value).attr("data-id")
            var imageUrl = $(value).find(".mbpho img").attr("src")
            var title = $(value).find(".wooscr .g").html()
            var like = $(value).find(".d2>span").html()
            var start = $(value).find(".d1>span").html()
            var user = {}
            var avatar = $(value).find(".f a>img").attr("src")
            var name = $(value).find(".f .p").html()
            var label = $(value).find(".f>p span").text()
            user = {avatar,name,label}
            arr.push({
                id,
                imageUrl,
                title,
                like,
                start,
                user
            })
        })
        return arr
    })
    await browser.close();
    ctx.body = res
}
module.exports = search